import Vue from "vue"
let prototype = Vue.prototype;

export default {
    typeText(listType, orderType) {
        switch (listType) {
            case "t1": return ["购买服务", "#3ABCF6"];
            case "t2":
                if (orderType == 1) {
                    return ["一口价", "#3ABCF6"];
                } else if (orderType == 2) {
                    return ["报价", "#8b9beb"];
                }
                return ["指派", "#f7a134"];
            case "t3": return ["货运", "#628BE7"];
        }
    },
    stateText(listType, orderType, state) {
        if (listType == 't1') {
            switch (state) {
                case 2: return ['服务中'];
                case 4: return ['待支付尾款'];
                case 5: return ['待客户确认'];
                case 6: return ['订单已完成'];
                case 7: return ['退款中'];
                case 8: return ['已退款'];
                case 9: return ['退款未通过'];
            }
        } else if (listType == 't2') {
            switch (state) {
                case 1: return ['待客户支付'];
                case 2: return ['服务中'];
                case 4: return ['待支付尾款'];
                case 5: return ['服务已完成，待确认'];
                case 6: return ['订单已完成'];
                case 7: return ['退款中'];
                case 8: return ['已退款'];
                case 9: return ['退款未通过'];
            }
        } else if (listType == 't3') {
            switch (state) {
                case 2: return ['待客户确认'];
                case 3: return ['已完成'];
            }
        }

        return ['未知'];
    },
    /**
     * 获取列表
     */
    getList(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    let list = [];
                    if (options.listType == "t1" || options.listType == "t2") {
                        response.data.forEach(item => {
                            list.push({
                                listType: options.listType,
                                id: item.id,
                                orderId: item.order,
                                cate: item.cate_name,
                                content: item.desc,
                                createTime: item.time,
                                serviceTime: item.times,
                                orderType: item.assign == 1 ? 3 : item.types,
                                state: item.status,
                                price: item.money,
                            });
                        });
                    } else if (options.listType == "t3") {
                        response.data.forEach(item => {
                            list.push({
                                listType: options.listType,
                                id: item.id,
                                orderId: item.order,
                                cate: "货运订单",
                                content: "",
                                createTime: item.times,
                                serviceTime: item.starttime,
                                orderType: 1,
                                state: item.status,
                                price: item.money,
                            });
                        });
                    }
                    return resolve(list);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 获取详情
     */
    getDetail(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    let order = {};
                    if (options.listType == "t1" || options.listType == "t2") {
                        order = {
                            listType: options.listType,
                            id: response.data.id,
                            orderId: response.data.order,
                            cate: response.data.cate_name,
                            content: response.data.desc,
                            createTime: response.data.time,
                            serviceTime: response.data.times,
                            orderType: response.data.assign == 1 ? 3 : response.data.types,
                            state: response.data.status,
                            price: response.data.money,
                            orderId: response.data.order,
                            address: {
                                address: response.data.address.address,
                                detail: response.data.address.doorplate,
                                name: response.data.address.name,
                                gender: response.data.address.sex == "男" ? 1 : 2,
                                mobile: response.data.address.mobil,
                                lng: response.data.address.lng,
                                lat: response.data.address.lat,
                            },
                        };
                        if (options.listType == "t1") {
                            order.images = response.data.goods.map((item) => item.thumbnail);
                        } else {
                            order.images = response.data.img;
                        }
                    } else if (options.listType == "t3") {
                        order = {
                            listType: options.listType,
                            id: response.data.id,
                            orderId: response.data.order,
                            cate: "货运订单",
                            createTime: response.data.times,
                            serviceTime: response.data.starttime,
                            pickupAddress: {
                                address: response.data.address_a.address,
                                detail: response.data.address_a.doorplate,
                                name: response.data.address_a.name,
                                gender: response.data.address_a.sex == "男" ? 1 : 2,
                                mobile: response.data.address_a.mobil,
                                lng: response.data.address_a.lng,
                                lat: response.data.address_a.lat,
                            },
                            unloadAddress: {
                                address: response.data.address_b.address,
                                detail: response.data.address_b.doorplate,
                                name: response.data.address_b.name,
                                gender: response.data.address_b.sex == "男" ? 1 : 2,
                                mobile: response.data.address_b.mobil,
                                lng: response.data.address_b.lng,
                                lat: response.data.address_b.lat,
                            },
                            state: response.data.status,
                            price: response.data.money,
                        };
                    }
                    return resolve(order);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 完成订单
     */
    finishOrder(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    return resolve(response);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 退款
     */
    refundOrder(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    return resolve(response);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 退款
     */
    appealOrder(options) {
        return new Promise((resolve, reject) => {
            prototype.$request(options.request).then(response => {
                if (response.code == 1) {
                    return resolve(response);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    }
}
